package com.activities;

import java.util.ArrayList;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.Utility.MessageManager;
import com.Utility.Utility;
import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.adapters.SecurePackageLogsAdapter;
import com.adapters.db.SecurePackageLogTableAdapter;
import com.controls.ws.SecurePackageLogWSControl;
import com.dialogs.LoadingDialog;
import com.events.WebServiceCommunicatorListener;
import com.objects.Contact;
import com.objects.SecurePackageLog;
import com.senditcertified.mail.R;

public class SecurePackageLogActivity extends BaseActivity implements
		WebServiceCommunicatorListener, OnClickListener {
	public static final String KEY_CONTACT_ITEM = "contact_item";
	private ListView mLstSecure;
	private TextView mTvNodata, mTvTitle;

	private SecurePackageLogsAdapter mAdapter;

	private SecurePackageLogWSControl mWSSecure;
	private SecurePackageLogTableAdapter mTASecure;
	private LoadingDialog mDialog;
	private Contact contact;

	@Override
	protected int initLayout() {
		return R.layout.activity_security_package_log;
	}

	@Override
	protected void initViews() {
		mTvNodata = (TextView) findViewById(R.id.tv_nodata);
		mTvTitle = (TextView) findViewById(R.id.tv_title);
		mLstSecure = (ListView) findViewById(R.id.list);
	}

	@Override
	protected void initComponents() {
		// TODO Auto-generated method stub]
		contact = getIntent().getParcelableExtra(KEY_CONTACT_ITEM);
		if (contact != null) {
			mTASecure = new SecurePackageLogTableAdapter(this);
			mTvTitle.setText("Secure Packages Sent to "
					+ contact.getEmailAddress());
			mWSSecure = new SecurePackageLogWSControl(this, this);
			ArrayList<SecurePackageLog> lst = mTASecure.getItems(contact
					.getID());
			if (lst != null && lst.size() > 0) {
				setAdapter(lst);
			}
			mWSSecure.getSecure(Utility.getStringPreference(this,
					Utility.KEY_LOGIN_TOKEN, ""), contact.getID());
		} else
			finish();

	}

	@Override
	protected void addListener() {
		findViewById(R.id.ln_back).setOnClickListener(this);

	}

	private void setAdapter(ArrayList<SecurePackageLog> lstItem) {
		if (lstItem != null && lstItem.size() > 0) {
			mAdapter = new SecurePackageLogsAdapter(this, lstItem);
			mTvNodata.setVisibility(View.GONE);
			mLstSecure.setVisibility(View.VISIBLE);
			mLstSecure.setAdapter(mAdapter);
		} else {
			mTvNodata.setVisibility(View.VISIBLE);
			mLstSecure.setVisibility(View.GONE);
			mAdapter = null;
		}

	}

	public void onBack(View v) {
		// TODO

	}

	@Override
	protected void onDestroy() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		super.onDestroy();
	}

	@Override
	public void onConnectionOpen(WebServiceFlag flag) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		if (mAdapter == null || mAdapter.getCount() < 1) {
			mDialog = new LoadingDialog(getParent());
			mDialog.setCancelable(false);
			mDialog.show();
		}

	}

	@Override
	public void onConnectionError(WebServiceFlag flag, String error) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		MessageManager.showToast(this, error);

	}

	@Override
	public void onConnectionDone(WebServiceFlag flag, String response) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		Log.e("EEEEEEEEEEEEE", response);

		if (flag == WebServiceFlag.GET_SECURE) {
			ArrayList<SecurePackageLog> lst = mWSSecure.parseSecure(response);
			setAdapter(lst);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.ln_back) {
			this.finish();
		}

	}

}
